// pages/shengyi/detail/detail.js
const util = require('../../../utils/util.js')
var WxParse = require('../../../wxParse/wxParse.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataDic: {},
    // 默认滚动视图高度是90vh，如果底部出现提示语就成80vh
    scrollHeight: '90vh',
    // 轮播图默认选中0
    current: 0,
    // isdeal  0未标注   1成交   2不成交
    isdeal: '',
    // 1联系，0没有联系
    islx: '',
    // 资产特有 
    issuo: '',
    // 是否是资产详情(false，代表不是资产)
    isZiChan: false,
    // 是资产就是资产的ID，是独享就是独享的ID
    ids: '',
    // 点击查看联系方式获取的密文(已解密)
    errNum: {},
    // 联系方式弹框显示隐藏
    showLx: false,
    // 是否显示标注弹框
    showBZ: false,
    // 标注没有成交回调
    showBZNo: false,
    // 不感兴趣弹框
    noxingquShow: false,
    // 如果有值就传递给详情接口，没值就算了
    wxpush: '',
    wxopenid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.wxpush) {
      this.setData({
        wxpush: options.wxpush
      })
    }

    if (options.wxopenid) {
      this.setData({
        wxopenid: options.wxopenid
      })
    }
    if (options.isZiChan == "true") {
      // 说明是资产进入
      let zcID = options.id
      this.setData({
        isZiChan: true,
        ids: zcID
      })
      this.getZCDataListDx(zcID)
    } else {
      // 说明是独享进入
      let dxID = options.id
      this.setData({
        isZiChan: false,
        ids: dxID
      })
      this.getDataListDx(dxID)

    }
  },
  // 获取资产
  getZCDataListDx(zcid) {
    var parame = {
      loginuid: wx.getStorageSync('FjLoginLog').UID,
      zcid: zcid,
      platform_type: '4',
    }
    if (this.data.wxpush && this.data.wxpush != undefined && this.data.wxpush != '') {
      parame['wxpush'] = this.data.wxpush
    }
    if (this.data.wxopenid && this.data.wxopenid != undefined && this.data.wxopenid != '') {
      parame['wxopenid'] = this.data.wxopenid
    }
  
    app.getDaoPostSearchNew('api/FeijiuApp/Feijiu_Get_Business_ZiChanInfo_v2024', parame, (item) => {
      if (item.code == 200) {
        let detailDic = item.data.data[0]
        // 把图片分割成数组
        let photo = util.getListArrWithStr(detailDic['photo'], '|')
        // 把视频分割成数组
        let video = util.getListArrWithStr(detailDic['video'], '|')
        // 把图片组装成{type:1,url:xxxx}样式
        let photoArr = []
        for (let image_url of photo) {
          let dic = {}
          dic['url'] = image_url
          dic['type'] = 1
          photoArr.push(dic)
        }
        // 把组装好的图片赋值给photos使用
        detailDic['photos'] = photoArr
        // 如果有视频，把视频组装成{type:2,url:xxxx}样式
        if (video.length) {
          let videoArr = []
          for (let video_url of video) {
            let dic = {}
            dic['url'] = video_url
            dic['type'] = 2
            videoArr.push(dic)
          }
          // 把组装好的图片与视频合并到一起
          detailDic['photos'] = photoArr.concat(videoArr)
        }
        detailDic['keywordsArr'] = util.getListArrWithStr(detailDic.keyword, ',')
        let islx = util.nullwithStr(item.data['islx'])
        let isdeal = util.nullwithStr(item.data['isdeal'])
        let issuo = util.nullwithStr(item.data['issuo'])
        // 处理标的详情标签
        let dd = detailDic['description'].replaceAll("http://", "https://")
        WxParse.wxParse('dd', 'html', dd, this, 5);
        this.setData({
          dataDic: detailDic,
          isdeal: isdeal,
          islx: islx,
          issuo: issuo
        })
        this.getScolViewHeight()
      } else {
        app.tishi(item.message)
      }
      wx.hideLoading()
    })
  },
  // 获取独享的详情
  getDataListDx(dxid) {
    let parame = {
      loginuid: wx.getStorageSync('FjLoginLog').UID,
      dxid: dxid,
      platform_type: '4',
    }
    if (this.data.wxpush && this.data.wxpush != undefined && this.data.wxpush != '') {
      parame['wxpush'] = this.data.wxpush
    }
    if (this.data.wxopenid && this.data.wxopenid != undefined && this.data.wxopenid != '') {
      parame['wxopenid'] = this.data.wxopenid
    }
    app.getDaoPostSearchNew('api/FeijiuApp/Feijiu_Get_Business_DuXiangInfo_v2024', parame, (item) => {
      if (item.code == 200) {
        let detailDic = item.data.data[0]
        let photo = util.getListArrWithStr(detailDic['photo'], '|')
          // 把图片组装成{type:1,url:xxxx}样式
          let photoArr = []
          for (let image_url of photo) {
            let dic = {}
            dic['url'] = image_url
            dic['type'] = 1
            photoArr.push(dic)
          }
        // 把组装好的图片赋值给photos使用
        detailDic['photos'] = photoArr
        detailDic['keywordsArr'] = util.getListArrWithStr(detailDic.keyword, ',')
        let islx = util.nullwithStr(item.data['islx'])
        let isdeal = util.nullwithStr(item.data['isdeal'])
        // 处理标的详情标签
        let dd = detailDic['description'].replaceAll("http://", "https://")
        WxParse.wxParse('dd', 'html', dd, this, 5);
        this.setData({
          dataDic: detailDic,
          isdeal: isdeal,
          islx: islx,
        })
        this.getScolViewHeight()
      } else {
        app.tishi(item.message)
      }
      wx.hideLoading()
    })
  },
  // 自定义轮播图的指示器
  bindchange(e) {
    this.setData({
      current: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  onUnload() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', { islx: this.data.islx, isdeal: this.data.isdeal, name: 'other' });
  },
  // 立即联系
  ljlxClick() {
    if (this.data.issuo == 0 && this.data.dataDic.shid == 4) {
      return
    }
    // 需要区分是独享还是资产
    if (this.data.isZiChan) {
      // 资产
      this.zichanLianXi()
    } else {
      // 独享
      this.duxiangLianXi()
    }
  },
  // 立即联系资产
  zichanLianXi() {
    // 1. 校验是否有权限查看
    let parame = {
      strUID: wx.getStorageSync('FjLoginLog').UID,
      strUpw: wx.getStorageSync('FjLoginLog').UPwd,
      strGqid: this.data.ids,
      platform_type: 4,
      isbusiness: 1 // 代表是生意频道
    }
    app.getDaoPostSearchNew('api/FeijiuApp/Feijiu_GetZcLxfs_Jm', parame, (item) => {
      if (item.code == 200) {
        var result = JSON.parse(app.aesDec(item.data.ZcInfo));
        console.log('result', result)
        if (result.ErrNum == 111) {
          // 需要弹框确认了
          this.setData({
            showLx: true,
            errNum: result
          })
        } else if (result.ErrNum == 11 || result.ErrNum == 1) {
          this.callPhone(result.Mobile, result.ComPhone, result.otherways)
        } else {
          // app.tishi('您没有权限查看该联系方式,了解更多请联系客服人员')
          wx.showModal({
            title: 'Feijiu网提示您',
            content: '对不起，您没有权限查看该公告，请您升级您的会员类型!',
            confirmText: '联系客服',
            complete: (res) => {
              if (res.confirm) {
                wx.makePhoneCall({
                  phoneNumber: item.data.ServiceNum,
                })
              }
            }
          })
        }
      } else {
        app.tishi(item.message)
      }
      wx.hideLoading()
    })
  },
  // 立即联系独享
  duxiangLianXi() {
    let parame = {
      loginuid: wx.getStorageSync('FjLoginLog').UID,
      dxid: this.data.ids,
      platform_type: '4',
      loginupw: wx.getStorageSync('FjLoginLog').UPwd,
      doaction: '3',
    }
    app.getDaoPostSearchNew('api/FeijiuApp/Feijiu_Get_Business_DuXiangLink_v2024', parame, (item) => {
      
      if (item.code == 200) {
        var result = JSON.parse(app.aesDec(item.data.ZcInfo));
        if (result.ErrNum == 111) {
          // 需要再次弹框确认
          this.setData({
            showLx: true,
            errNum: result
          })
          wx.hideLoading()
        } else if (result.ErrNum == 11 || result.ErrNum == 1) {
          wx.hideLoading()
          // 可以直接进行拨打
          this.callPhone(result.Mobile, result.ComPhone, result.otherways)
        } else {
          // app.tishi('您没有权限查看该联系方式,了解更多请联系客服人员')
          wx.showModal({
            title: 'Feijiu网提示您',
            content: '对不起，您没有权限查看该公告，请您升级您的会员类型!',
            confirmText: '联系客服',
            complete: (res) => {
              if (res.confirm) {
                wx.makePhoneCall({
                  phoneNumber: item.data.ServiceNum,
                })
              }
            }
          })
        }
      } else {
        app.tishi(item.message)
      }
     
    })
  },
  // 拨打电话方法
  callPhone(phoneNumber, zuoji, other) {
    let phone = ''
    if (phoneNumber) {
      phone = phoneNumber
    } else if (zuoji) {
      phone = zuoji
    } else if (other) {
      phone = '复制' + other
    } else {
      app.tishi('后台返回数据中3个电话字段都没有值')
      return
    }
    wx.showActionSheet({
      itemList: [phone],
      alertText: '拨打电话',
      success(res) {
        if (phone == '复制' + other) {
          wx.setClipboardData({
            data: other,
            success: () => {
              app.tishi('复制成功')
            }
          })
        } else {
          wx.makePhoneCall({
            phoneNumber: phone,
          })
        }

      },
    })
  },
  // 拨打电话弹框的回调按钮点击
  myevent(e) {
    // 无论点击取消还是点击立即联系都要取消弹框
    this.setData({
      showLx: false
    })
    if (e.detail.index == 0) {
      // 取消,此时不需要做任何操作!!!
    } else {
      // 点击了立即联系
      if (this.data.isZiChan) {
        this.zcphoneCall()
      } else {
        this.phoneCall()
      }
    }
  },
  // 独享立即联系弹框回调扣除特权
  phoneCall() {
    let parame = {
      loginuid: wx.getStorageSync('FjLoginLog').UID,
      dxid: this.data.ids,
      platform_type: '4',
      loginupw: wx.getStorageSync('FjLoginLog').UPwd,
      doaction: '1'
    }
    app.getDaoPostSearchNew('api/FeijiuApp/Feijiu_Get_Business_DuXiangLink_v2024', parame, (item) => {
      if (item.code == 200) {
        var result = JSON.parse(app.aesDec(item.data.ZcInfo));
        if (result.ErrNum == 111) {
          app.tishi('result.ErrNum', result.ErrNum)
        } else if (result.ErrNum == 11 || result.ErrNum == 1) {
          this.callPhone(result.Mobile)
          this.islx = 1
          this.setData({
            islx: this.islx
          })
          wx.hideLoading()
        } else {
          // app.tishi('您没有权限查看该联系方式,了解更多请联系客服人员')
          wx.showModal({
            title: 'Feijiu网提示您',
            content: '对不起，您没有权限查看该公告，请您升级您的会员类型!',
            confirmText: '联系客服',
            complete: (res) => {
              if (res.confirm) {
                wx.makePhoneCall({
                  phoneNumber: item.data.ServiceNum,
                })
              }
            }
          })
        }
      } else {
        app.tishi(item.message)
      }
     
    })
  },
  // 资产立即联系扣除特权
  zcphoneCall() {
    let parame = {
      strUID: wx.getStorageSync('FjLoginLog').UID,
      strUpw: wx.getStorageSync('FjLoginLog').UPwd,
      strGqid: this.data.ids,
      platform_type: 4,
      isbusiness: 1 // 代表是生意频道
    }
    app.getDaoPostSearchNew('api/FeijiuApp/Feijiu_GetZcLxfsInfo_Jm', parame, (item) => {
      if (item.code == 200) {
        var result = JSON.parse(app.aesDec(item.data.ZcInfo));
        console.log('result', result)
        if (result.ErrNum == 111) {
          // 不需要再次弹框了就
          wx.hideLoading()
        } else if (result.ErrNum == 11 || result.ErrNum == 1) {
          wx.hideLoading()
          this.callPhone(result.Mobile)
          this.islx = 1
          this.setData({
            islx: this.islx
          })
          this.getScolViewHeight()
        } else {
          // app.tishi('您没有权限查看该联系方式,了解更多请联系客服人员')
          wx.showModal({
            title: 'Feijiu网提示您',
            content: '对不起，您没有权限查看该公告，请您升级您的会员类型!',
            confirmText: '联系客服',
            complete: (res) => {
              if (res.confirm) {
                wx.makePhoneCall({
                  phoneNumber: item.data.ServiceNum,
                })
              }
            }
          })
        }
      } else {
        app.tishi(item.message)
      }
  
    })
  },
  // 获取高度
  getScolViewHeight() {
    // wx:if="{{!(isdeal == 0 && islx == 0)}}"
    if (!(this.data.isdeal == 0 && this.data.islx == 0)) {
      // 展示提示语
      this.setData({
        scrollHeight: '85vh'
      })
    } else {
      // 不展示提示语
      this.setData({
        scrollHeight: '90vh'
      })
    }
  },
  /******标注************/
  biaozhuClick() {
    this.setData({
      showBZ: true
    })
  },
  // 标注确定回调
  bzclick(e) {
    let dic = e.detail
    // 代表选中的是标注
    if (dic.index == 1) {
      // 顺利成交
      this.setData({
        showBZ: false
      })
      console.log('顺利成交--调用接口', dic.index)
      // 调用接口
      this.biaoZhuAip(dic.index, '0')
    } else {
      // 没有成交，弹出没有成交理由
      this.setData({
        showBZNo: true
      })
    }
  },
  // 没有标注回调
  nobzclick(e) {
    // 选中的是非标注，标注失败的原因
    // 选中原因以后得回调
    this.biaoZhuAip(2, e.detail.index)
    this.setData({
      showBZ: false,
      showBZNo: false
    })
  },
  // 标注没有成交关闭
  closexqTwoClick() {
    this.setData({
      showBZNo: false
    })
  },
  // 标注接口调用()
  biaoZhuAip(isdeal, reason_item) {
    wx.showLoading({
      title: '',
    })
    let parame = {
      platform_type: 4,
      //  1资产  2拍卖  3独享
      infotype: this.data.isZiChan ? 1 : 3,
      loginuid: wx.getStorageSync('FjLoginLog').UID,
      // 编号
      infoid: this.data.ids,
      // 1顺利成交   2没有成交
      isdeal: isdeal,
      // 原因：
      reason_item: reason_item,

    }
    app.getDaoPostSearchNew('api/FeijiuApp/Feijiu_Dimension_BusinessInfo_v2024', parame, (item) => {
      if (item.code == 200) {
        if (item.data.code == 2) {
          app.tishi(item.data.tip)
        } else {
          // 0未标注   1成交   2不成交
          this.data.isdeal = isdeal
          this.getScolViewHeight()
          // 刷新界面
          this.setData({
            isdeal: this.data.isdeal
          })
          wx.hideLoading()
        }
       
      } else {
        app.tishi(item.message)
      }
     
    })

  },
  /*****************不感兴趣*************** */
  noInterestClick() {
    this.setData({
      noxingquShow: true,
    })
  },
  // 不感兴趣的回调
  xingquSure(e) {
    wx.showLoading({
      title: '',
    })
    let parame = {
      platform_type: 4,
      //  1资产  2拍卖  3独享
      infotype: this.data.isZiChan ? 1 : 3,
      loginuid: wx.getStorageSync('FjLoginLog').UID,
      // 生意的编号
      infoid: this.data.ids,
      // 拍卖信息code
      infocode: '',
      // 生意的发布时间
      infotime: this.data.isZiChan ? this.data.dataDic.fbdate : this.data.dataDic.fbtime,
      // 原因：1、价格高，不赚钱    2、地区太远，不赚钱    3、量太少，不赚钱    4、不感兴趣
      reason_item: e.detail.idd,
    }
    app.getDaoPostSearchNew('api/FeijiuApp/Feijiu_Uninterested_BusinessInfo_v2024', parame, (item) => {
      if (item.code == 200) {
        // 获取当前
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('acceptDataFromOpenedPage', { 'name': 'noxq' });
        wx.navigateBack()
        wx.hideLoading()
      } else {
        app.tishi(item.message)
      }
    })
    this.setData({
      noxingquShow: false
    })
  },
  // 不感兴趣关闭
  closexqClick(e) {
    this.setData({
      noxingquShow: false,
      showBZ: false
    })
  },


})